#-------------------------------
# VisualK-OS                    
#    Module: Thor Driver [v1.0] 
#    Author: Kevin Slaughter    
#-------------------------------

"""
[Revision History]
    v2.0: Standarization, misc. optimizations.
    v1.0: Initial implementation.
"""



### Lib/Header import
from VKCC_Constants import *
import globals, serial

### Obj: Thor COMMS --- [Drum lighting module]
class cThor(object):
    ### [General]
    ID = "Thor"                        # Debug reference
    devPath = "/dev/vkosTHOR"          # Device path [Linux]
    isAvailable = False		        # Status indicator
    isHandshook = False	                # ^      ^       ^
    isMuted = False		        # All lights off, regardless of triggering
    numLights = 7                      # Zero-based (Current cfg: 8x[RGB])
    serComms = serial                  # Serial connection to device
    
    ### [Device-specific properties]
    numTriggers = 7			    # Zero-based number of trigger inputs
    colors = [1,2,3,4,5,6,7,8]		    # VK Constant color of each lamp
    rtLevels = \
        [1024,1024,1024,1024,1024,1024,1024,1024]	# (near)Real-Time piezo trigger levels
    thresholds = [80,81,82,83,84,85,86,87] # Threshold at which a light is activated



    ### [Functions]
    ### cfgThreshold(): Sets threshold level of x-trigger on Thor.
    def cfgThreshold(self, iTrigger, iNewLevel):
        # Sanity checks
        if ((iTrigger<0) or (iTrigger>self.numLights)): return  # Invalid trigger index. Exit.
        
        # Trigger thresholds must be 0~1024 (min/max analog input values)
        if (iNewLevel<0): iNewLevel = 0
        elif (iNewLevel>1024): iNewLevel = 1024
        
        # Apply new level
        self.thresholds[iTrigger] = iNewLevel
        return


    ### cfgColor(): Configure an RGB LED lamp
    def cfgColor(self, iLight, iNewColor):
        # Sanity checks
        if((iLight<0) or (iLight>self.numLights)): return   # Invalid trigger index. Exit
        if(iNewColor <= VKC_YELLOW): return		     # Invalid light color. Exit
        
        # Apply new color
        self.colors[iLight] = iNewColor
        return


    ### getNumTriggers(): Returns the amount of physical piezo triggers
    def getNumTriggers(self): return self.numTriggers


    ### updateColor(): Sends new color settings to Thor
    def updateColor(self, iTrig):
        # TODO: Send color
        return


    ### updateThreshold(): Sends new threshold levels to Thor
    def updateThreshold(self, iTrig):
        # TODO: Send new level
        return
